package com.koron.css2.clientCenter.impl;

import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.CostBean;
import com.koron.css2.baseConfig.mapper.CostMapper;
import com.koron.css2.clientCenter.bean.MeterReaderBean;
import com.koron.css2.clientCenter.mapper.ValvePubMapper;
import com.koron.css2.clientCenter.mapper.ValveUserMapper;
import com.koron.css2.clientCenter.queryBean.ValveUserQuery;
import com.koron.css2.clientCenter.vo.ValveUserQueryVo;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author cd
 * 关阀详情-欠费用户查询
 */
public class ValveCloseUserQuery implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ValveUserQuery query = JsonUtils.objectToPojo(req.getData(), ValveUserQuery.class);
            ValvePubMapper pubMapper = factory.getMapper(ValvePubMapper.class);
            //查询表的阀控类型
            ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
            // PageHelper.startPage(query.getPage(), query.getPageCount());
            query.setStart((query.getPage() - 1) * query.getPageCount());
            query.setEnd(query.getPage() * query.getPageCount());


            CostMapper Cmapper = factory.getMapper(CostMapper.class);

            List<CostBean> zhList = Cmapper.selectCostForCompreHensive();
            List<String> zh = new ArrayList();
            //获取综合水费 费用列表
            if(CollectionUtils.isNotEmpty(zhList)) {
                zh = zhList.stream().map(CostBean::getCostNo).collect(Collectors.toList());
            }
            //综合水费 费用类型处理
            if(CollectionUtils.isNotEmpty(query.getCostNos())) {
                String zhCost = query.getCostNos().stream().filter(t->"zh".equals(t)).findFirst().orElse(null);
                if(zhCost != null) {
                    if(zh != null)
                        query.getCostNos().addAll(zh);
                    query.getCostNos().remove("zh");
                }
            }

            List<ValveUserQueryVo> list = valveUserMapper.getUserBill(query);
            if (null == list || list.size() == 0) {
                @SuppressWarnings("rawtypes")
                MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
                PageInfo pageInfo = new PageInfo();
                info.setData(pageInfo);
                return info;
            }
            List<String> idList = list.stream().map(ValveUserQueryVo::getUserNo).collect(Collectors.toList());
            Map<String, MeterReaderBean> meterReaderBeanMap = new HashMap<>();
            List<MeterReaderBean> userList = valveUserMapper.selectMeterReaderList(idList);
            for (MeterReaderBean bean : userList) {
                meterReaderBeanMap.put(bean.getUserNo(), bean);
            }
//            Map<String, MeterReaderBean> meterReaderBeanMap = userList.stream().collect(Collectors.toMap(MeterReaderBean::getUserNo, meterReaderBean -> meterReaderBean));
            //获取阀门状态字典
            Map<String, String> dics = Tools.mapDicByCode(factory, "MVS");

            for (ValveUserQueryVo vo : list) {
                if (null != meterReaderBeanMap.get(vo.getUserNo())) {
                    MeterReaderBean bean = meterReaderBeanMap.get(vo.getUserNo());
                    CommonUtils.copyPropertiesIgnoreNull(bean, vo);
                    vo.setHandler(vo.getMeterReader());
                }

                //是否为阀控表
                if (null != vo.getValveControlFlag() && "1".equals(vo.getValveControlFlag())) {
                    vo.setValveControlFlag("是");
                } else {
                    vo.setValveControlFlag("否");
                }

                //是否为远程表
                if (null != vo.getRemoteFlag() && "1".equals(vo.getRemoteFlag())) {
                    vo.setRemoteFlag("是");
                } else {
                    vo.setRemoteFlag("否");
                }

                //是否欠费
                if (vo.getArrearage() != null && 1 == vo.getArrearage().compareTo(BigDecimal.ZERO)) {
                    vo.setFeeStatus("是");
                } else {
                    vo.setFeeStatus("否");
                }

                //通过字典表替换阀门状态为中文
                if (null != vo.getValveStatus() && null != dics.get(vo.getValveStatus())) {

                    String valveStatusName = dics.get(vo.getValveStatus());
                    vo.setValveStatusName(valveStatusName);
                }
            }

            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            PageInfo pageInfo = new PageInfo(list);
            info.setData(pageInfo);
            return info;

        } catch (Exception e) {
            logger.error("欠费用户查询失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }

    }

}
